package com.zaofan.mobile.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.zaofan.bean.Comment;
import com.zaofan.bean.Reply;
import com.zaofan.bean.Shop;
import com.zaofan.constant.StateConstant;
import com.zaofan.constant.UrlConstant;
import com.zaofan.service.CommentService;
import com.zaofan.service.ReplyService;
import com.zaofan.service.ShopService;
import com.zaofan.utils.JsonUtils;

@WebServlet("/mobile/shop")
public class ShopController extends BaseServlet {

	private static final long serialVersionUID = 5884947841079014459L;
	private ShopService shopService = new ShopService();
	private ReplyService replyService = new ReplyService();
	private CommentService commentService = new CommentService();

	protected void info(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String temp = request.getParameter("id");
			int id = Integer.valueOf(temp);
			Shop shop = shopService.findById(id);
			if (shop == null) {
				JsonUtils.writeBeanFailJson("参数错误", response);
				return;
			}
			System.err.println("get shop info : id = " + id);
			com.zaofan.mobile.bean.Shop s = new com.zaofan.mobile.bean.Shop();
			BeanUtils.copyProperties(s, shop);
			Calendar now = Calendar.getInstance();
			Calendar beginTime = Calendar.getInstance();
			Calendar endTime = Calendar.getInstance();
			s = new com.zaofan.mobile.bean.Shop();
			BeanUtils.copyProperties(s, shop);
			s.setShopImage(UrlConstant.HOST_URL + s.getShopImage());
			beginTime.set(Calendar.HOUR_OF_DAY,
					Integer.valueOf(shop.getBeginTime().split(":")[0]));
			beginTime.set(Calendar.MINUTE,
					Integer.valueOf(shop.getBeginTime().split(":")[1]));
			beginTime.set(Calendar.SECOND,
					Integer.valueOf(shop.getBeginTime().split(":")[2]));
			endTime.set(Calendar.HOUR_OF_DAY,
					Integer.valueOf(shop.getEndTime().split(":")[0]));
			endTime.set(Calendar.MINUTE,
					Integer.valueOf(shop.getEndTime().split(":")[1]));
			endTime.set(Calendar.SECOND,
					Integer.valueOf(shop.getEndTime().split(":")[2]));
			if (now.before(beginTime) || now.after(endTime)) {
				s.setIsTimeOut(0);
				if (shop.getTimeoutImage() != null
						&& !"".equals(shop.getTimeoutImage())) {
					s.setShopImage(UrlConstant.HOST_URL
							+ shop.getTimeoutImage());
				}
			} else {
				s.setIsTimeOut(1);
			}
			JsonUtils.writeSuccessJson("查询成功", s, response);
			return;
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeBeanFailJson("参数错误", response);
		}
		JsonUtils.writeBeanFailJson("获取失败", response);
	}

	/**
	 * app 首页 商家列表接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shop(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			Map<String, Object> params = new HashMap<>();
			Enumeration<String> enumeration = request.getParameterNames();
			while (enumeration.hasMoreElements()) {
				String name = enumeration.nextElement();
				if ("action".equals(name)) {
					continue;
				}
				Object value = request.getParameter(name);
				params.put(name, value);
			}
			params.put("coverType", 0);
			params.put("state", StateConstant.STATE_SHOW);
			List<Shop> shops = shopService.findAll(params);
			List<com.zaofan.mobile.bean.Shop> target = new ArrayList<>();
			com.zaofan.mobile.bean.Shop s = null;
			Calendar now = Calendar.getInstance();
			Calendar beginTime = Calendar.getInstance();
			Calendar endTime = Calendar.getInstance();
			for (Shop shop : shops) {
				s = new com.zaofan.mobile.bean.Shop();
				BeanUtils.copyProperties(s, shop);
				s.setShopImage(UrlConstant.HOST_URL + s.getShopImage());
				beginTime.set(Calendar.HOUR_OF_DAY,
						Integer.valueOf(shop.getBeginTime().split(":")[0]));
				beginTime.set(Calendar.MINUTE,
						Integer.valueOf(shop.getBeginTime().split(":")[1]));
				beginTime.set(Calendar.SECOND,
						Integer.valueOf(shop.getBeginTime().split(":")[2]));
				endTime.set(Calendar.HOUR_OF_DAY,
						Integer.valueOf(shop.getEndTime().split(":")[0]));
				endTime.set(Calendar.MINUTE,
						Integer.valueOf(shop.getEndTime().split(":")[1]));
				endTime.set(Calendar.SECOND,
						Integer.valueOf(shop.getEndTime().split(":")[2]));
				if (now.before(beginTime) || now.after(endTime)) {
					s.setIsTimeOut(0);
					if (shop.getTimeoutImage() != null
							&& !"".equals(shop.getTimeoutImage())) {
						s.setShopImage(UrlConstant.HOST_URL
								+ shop.getTimeoutImage());
					}
				} else {
					s.setIsTimeOut(1);
				}
				target.add(s);
			}
			Long count = shopService.count(params);
			JsonUtils.writeSuccessPageJson("查询成功", target, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * app 首页 商家评论接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void comment(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			String id = request.getParameter("shopId");
			Integer shopId = Integer.valueOf(id);
			List<Comment> comments = commentService.findByShopId(shopId);
			List<com.zaofan.mobile.bean.Comment> datas = new ArrayList<>();
			com.zaofan.mobile.bean.Comment comment = null;
			for (Comment temp : comments) {
				comment = new com.zaofan.mobile.bean.Comment();
				BeanUtils.copyProperties(comment, temp);
				if (comment.getIsReply() == 1) {
					Reply reply = replyService.findByCommentId(comment.getId());
					comment.setReply(reply);
				}
				if (comment.getUserPhoto() != null
						&& !"".equals(comment.getUserPhoto())) {
					comment.setUserPhoto(UrlConstant.HOST_URL
							+ comment.getUserPhoto());
				}
				datas.add(comment);
			}
			JsonUtils.writeSuccessJson("查询成功", datas, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

	/**
	 * app 首页 商家列表接口
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shop_lable(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			String begin = request.getParameter("begin");
			String end = request.getParameter("end");
			String builderId = request.getParameter("builderId");
			List<Shop> shops = shopService.findByBuilderIdWithPage(builderId,
					begin, end);
			List<com.zaofan.mobile.bean.Shop> target = new ArrayList<>();
			com.zaofan.mobile.bean.Shop s = null;
			for (Shop shop : shops) {
				s = new com.zaofan.mobile.bean.Shop();
				BeanUtils.copyProperties(s, shop);
				target.add(s);
			}
			Long count = shopService.countByBuilderId(Integer
					.valueOf(builderId));
			JsonUtils.writeSuccessPageJson("查询成功", target, count, response);
		} catch (Exception e) {
			e.printStackTrace();
			JsonUtils.writeListFailJson("参数错误!!", response);
		}
		JsonUtils.writeListFailJson("查询失败!!", response);
	}

}
